<?php

class ClassTypeController extends GxController {

    public function filters() {
        return array('rights');
    }

    public function actionView($id) {
        $this->render('view', array(
            'model' => $this->loadModel($id, 'ClassType'),
        ));
    }

    public function actionCreate() {
        $model = new ClassType;
        $this->saveClassType($model);
        $this->render('create', array('model' => $model));
    }

    public function actionUpdate($id) {
        $model = $this->loadModel($id, 'ClassType');
        $this->saveClassType($model);
        $this->render('update', array(
            'model' => $model,
        ));
    }

    public function actionDelete($id) {
        if (Yii::app()->getRequest()->getIsPostRequest()) {
            $model = $this->loadModel($id, 'ClassType');
            $uploadeDir = getUploadDirBackend() . 'class-type-images/';
            $updateData = $model->findByAttributes(array('id' => $id));
            if ($updateData->photo <> "" && file_exists($uploadeDir . $updateData->photo)) {
                unlink($uploadeDir . $updateData->photo);
            }

            $this->loadModel($id, 'ClassType')->delete();
            if (!Yii::app()->getRequest()->getIsAjaxRequest())
                $this->redirect(array('admin'));
        } else
            throw new CHttpException(400, Yii::t('app', 'Your request is invalid.'));
    }

    public function actionIndex() {
        $dataProvider = new CActiveDataProvider('ClassType');
        $this->render('index', array(
            'dataProvider' => $dataProvider,
        ));
    }

    public function actionAdmin() {
        $model = new ClassType('search');
        $statusChangeUrl = Yii::app()->createUrl('classtype/statuschange/');
        $model->unsetAttributes();

        if (isset($_GET['ClassType']))
            $model->setAttributes($_GET['ClassType']);
        $this->render('admin', array(
            'model' => $model,
            'statusChangeUrl' => $statusChangeUrl,
        ));
    }

    public function actionStatuschange() {
        $model = $this->loadModel($_POST['id'], 'ClassType');
        $model->status = $_POST['status'];
        $model->save();
    }

    private function saveClassType($model) {
        $uploadeDir = BASE_PATH_CLASSTYPE;
        $db_logo = '';
        if (isset($_POST['ClassType'])) {
            $db_logo = $model->photo;
            $model->setattributes($_POST['ClassType']);
            $model->setAttribute('photo ', $db_logo);
            (($model->isNewRecord) ? $model->setAttribute('create_time', time()) : $model->setAttribute('update_time', time()));
            if ($model->validate()) {
                $transaction = Yii::app()->getDb()->beginTransaction();
                try {
                    if (isset($_FILES['ClassType']['name']['photo']) && !empty($_FILES['ClassType']['name']['photo'])) {
                        if (!empty($model->photo)) {
                            if (file_exists($uploadeDir . $model->photo)) {
                                unlink($uploadeDir . $model->photo);
                            }
                        }
                        $model->photo = CUploadedFile::getInstance($model, 'photo');
                        $fileInfo = pathinfo($model->photo);
                        $ext = $fileInfo['extension'];
                        $image = $_FILES['ClassType']['tmp_name'];
                        $mainTempImages = $image['photo'];
                        $rfilename = guid() . "." . strtolower($ext);
                        $uploadnewDir = $uploadeDir . $rfilename;
                        $model->photo->saveAs($uploadnewDir);
                        $image = Yii::app()->image->load($uploadnewDir);
                        $image->resize(USER_IMAGE_WIDTH, USER_IMAGE_HEIGHT, Image::NONE);
                        $image->save();
                        $model->photo = $rfilename;
                        $model->setAttribute('photo', $model->photo);
                    }
                    $transaction->commit();
                } catch (Exception $e) {
                    $transaction->rollback();
                    throw $e;
                }
                if ($model->save()) {
                    $this->redirect(array('view', 'id' => $model->id));
                }
            }
        }
    }

}
